#pragma once
#include <memory>
#include <mutex>
#include <map>
#include <condition_variable>
#include "Pal/VideoEnc.h"
#include "AVFrame.h"
#include "frame_common.h"
namespace Media{

    typedef struct{
        int m_width;
        int m_height;
        int m_frameRate;
        int m_bitRate;
        std::string m_codec; //H264/H265
    }VEncoderConfig;

    class VEncoder{
    public:
        using Ptr = std::shared_ptr<VEncoder>;
        using FrameConsumerId = uint32_t;

        VEncoder(int channel, const VEncoderConfig& config);
        ~VEncoder();

        bool Start();
        bool Stop();

        // bool InputAVFrame(AVFrame::Ptr frame);
        bool InputAVFrame(void* frame);

        FrameConsumerId AddConsumer(std::function<void (const FrameCommon::Frame::Ptr&)> consumer);
        bool RemoveConsumer(FrameConsumerId id);

        int GetChannel();
    private:
        bool init();
        void packetProc();
        std::map<FrameConsumerId, std::function<void (const FrameCommon::Frame::Ptr&)>> m_frameConsumers;
        uint32_t m_frameConsumerIndex;
        std::recursive_mutex m_mux;

        std::mutex m_cvMux;
        std::condition_variable m_cv;

        VideoEncoder *m_encoder;
        VEncoderConfig m_config;
        int m_channel;
        bool m_running;
    };
}